package com.acme.domain.client;

import com.acme.exceptions.ClientExistsException;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class Bank implements Serializable {
    private final Set<Client> clients = new HashSet<Client>();

    public Set<Client> getClients() {
        return Collections.unmodifiableSet(clients);
    }

    public void addClient(final Client client) throws ClientExistsException {
        // XXX [MY] what is about "clients.contains(client)" ?
        if (getClientByName(client.getName()) != null) {
            String message = String.format("Client with name: %s has already in the bank", client.getName());
            throw new ClientExistsException(message, client);
        }
        clients.add(client);
    }

    public Client getClientByName(final String name) {
        for (Client client : clients) {
            if (client.getName().equals(name)) {
                return client;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return String.format("Bank{clients=%s}", clients);
    }
}